package server.main.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import server.main.config.cache.MybatisPlusRedisCache;
import server.main.pojo.Account;
import server.main.pojo.Doctor;
import server.main.pojo.Role;
import server.main.service.AccountRoleSercice;
import server.main.service.AccountService;
import server.main.service.DoctorService;

import java.util.HashMap;
import java.util.Map;

/**
 * @auther MingHsZzz
 * @Date 2022/10/10  14:22
 * @project main
 * @description
 */

@RequestMapping("/account")
@RestController
public class AccountController {


   @Autowired

   MybatisPlusRedisCache mybatisPlusRedisCache;

   @Autowired
   AccountService accountService;

   @Autowired
   AccountRoleSercice accountRoleSercice;

   @Autowired
   DoctorService doctorService;

   @GetMapping("/getallaccount")
   public Map<String,Object> getAllAccountByPage(@RequestParam(required = false, defaultValue = "") String jobnum,
                                           @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                           @RequestParam(required = false, defaultValue = "10") Integer pageSize){
      System.out.println("<----------------获取Account全部信息-------------------->");

      Map<String,Object> map = new HashMap<>();
//        Result.success(roleService.page(new Page<>(pageNum, pageSize), Wrappers.<Role>lambdaQuery().like(Role::getName, name)));
      Page<Account> completeAccMsgByPage = accountService.getCompleteAccMsgByPage(jobnum,pageNum,pageSize);

//      System.out.println(completeAccMsgByPage.getRecords().toString());

      map.put("code",200);
//      map.put("code2",200);
      map.put("data",completeAccMsgByPage);
      for (Account account : completeAccMsgByPage.getRecords()) {
         account.setDoctor(doctorService.getDoctorByJobnum(account.getJobnum()));
      }


//      System.out.println("recodes" + completeAccMsgByPage.getRecords());
      System.out.println(completeAccMsgByPage.getRecords());


      return map;

   }


   @PostMapping("/updateaccount")
   public Map<String,Object> updateAccountRole(@RequestBody String account){

      Map<String, Object> map = new HashMap<>();

      //返回的role对象中未存在的删除
      JSONObject parse = (JSONObject)JSONObject.parse(account);

      JSONArray jsonArray = (JSONArray)parse.get("a");
      JSONObject jsonObject = (JSONObject)parse.get("b");
      System.out.println("Account传回来的数据："+parse.toJSONString());

      Account preacc = null;
      Account nowacc = JSONObject.toJavaObject(jsonObject,Account.class);
      for (Object o : jsonArray) {
         if(o instanceof String){
            continue;
         }
         JSONObject temp = (JSONObject)o;
         if(temp.get("id") == nowacc.getId()){
//                if(temp)
            preacc = JSONObject.toJavaObject(temp,Account.class);
         }
      }
      System.out.println("原始Acc：" + preacc);
      System.out.println("当前Acc：" + nowacc);

      mybatisPlusRedisCache.clear("server.main.mapper.AccountMapper");
      mybatisPlusRedisCache.clear("server.main.mapper.AccountRoleMapper");
      mybatisPlusRedisCache.clear("server.main.mapper.RoleMapper");
      mybatisPlusRedisCache.clear("server.main.mapper.PermissionMapper");
      mybatisPlusRedisCache.clear("server.main.mapper.RolePermissionMapper");
//      rolePermissionService.updatePermissionBatch(prerole,nowrole);
      accountRoleSercice.updateRoleBatch(preacc,nowacc);



      map.put("code",200);
      map.put("msg","更新成功");

      return map;

   }


   @PostMapping("/delete")
   public Map<String,Object> dodelete(@RequestParam String jobnum){
      Map<String,Object> map = new HashMap<>();
      accountService.deleteAccountByJonbnum(jobnum);
      mybatisPlusRedisCache.clear("server.main.mapper.AccountMapper");
      mybatisPlusRedisCache.clear("server.main.mapper.AccountRoleMapper");
      mybatisPlusRedisCache.clear("server.main.mapper.RoleMapper");
      mybatisPlusRedisCache.clear("server.main.mapper.PermissionMapper");
      mybatisPlusRedisCache.clear("server.main.mapper.RolePermissionMapper");

      map.put("code",200);
      map.put("msg","删除成功");

      return map;
   }











}
